-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for encoding/decoding compressed EC keys #57
Open
huskcasaca
wants to merge
6
commits into
whyoleg:main
Choose a base branch
from
huskcasaca:dev-compressed-format
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
huskcasaca
commented
Dec 13, 2024
Comment on lines
+211
to
+243
internal fun ECParameterSpec.decodePoint(bytes: ByteArray): ECPoint { | ||
val fieldSize = curveOrderSize() | ||
return when (bytes[0].toInt()) { | ||
0x02, // compressed evenY | ||
0x03, // compressed oddY | ||
-> { | ||
check(bytes.size == fieldSize + 1) { "Wrong compressed key size ${bytes.size}" } | ||
val p = (curve.field as ECFieldFp).p | ||
val a = curve.a | ||
val b = curve.b | ||
val x = BigInteger(1, bytes.copyOfRange(1, bytes.size)) | ||
var y = x.multiply(x).add(a).multiply(x).add(b).mod(p).modSqrt(p) | ||
if (y.testBit(0) != (bytes[0].toInt() == 0x03)) { | ||
y = p.subtract(y) | ||
} | ||
ECPoint(x, y) | ||
} | ||
0x04, // uncompressed | ||
-> { | ||
check(bytes.size == fieldSize * 2 + 1) { "Wrong uncompressed key size ${bytes.size}" } | ||
val x = bytes.copyOfRange(1, fieldSize + 1) | ||
val y = bytes.copyOfRange(fieldSize + 1, fieldSize + 1 + fieldSize) | ||
ECPoint(BigInteger(1, x), BigInteger(1, y)) | ||
} | ||
else -> error("Unsupported key type ${bytes[0].toInt()}") | ||
} | ||
} | ||
|
||
internal fun BigInteger.modSqrt(p: BigInteger): BigInteger { | ||
check(p.testBit(0) && p.testBit(1)) { "Unsupported curve modulus" } // p ≡ 3 (mod 4) | ||
return modPow(p.add(BigInteger.ONE).shiftRight(2), p) // Tonelli-Shanks | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BC is added as a provider without some API access so it requires manually calculating y
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR add support for
Compressed
andUncompressed
(default) format underRAW
EC Public Key.